package com.gy.studyonlineapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gy.studyonlineapp.R;
import com.gy.studyonlineapp.entity.Video;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class VideoAdapter extends ArrayAdapter<Video> {

    private int resourceId;

    private Context context;

    public VideoAdapter(Context context, int resourceId, List<Video> videos) {
        super(context, resourceId, videos);
        this.context = context;
        this.resourceId = resourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Video item = getItem(position);
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(resourceId, null);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.jzvdStd = convertView.findViewById(R.id.jz_video);
        viewHolder.jzvdStd.setUp(
                item.getUrl(),
                item.getTitle(),
                Jzvd.SCREEN_NORMAL);
        Picasso.with(convertView.getContext())
                .load(item.getPostImg())
                .into(viewHolder.jzvdStd.posterImageView);
        return convertView;
    }

    class ViewHolder {
        JzvdStd jzvdStd;
    }
}